import {
	validatenull
} from '../validate'

const keyName = 'Ring-'
/**
 * 存储localStorage
 */
export const setStore = (params = {}) => {
	let {
		name,
		content,
		type
	} = params
	name = keyName + name
	let obj = {
		dataType: typeof(content),
		content: content,
		type: type,
		datetime: new Date().getTime()
	}
	if (type) uni.setStorageSync(name, JSON.stringify(obj))
	else uni.setStorageSync(name, JSON.stringify(obj))
}
/**
 * 获取localStorage
 */

export const getStore = (params = {}) => {
	let {
		name,
		debug
	} = params
	name = keyName + name
	let obj = {},
		content
	obj = uni.getStorageSync(name)
	if (validatenull(obj)) obj = uni.getStorageSync(name)
	if (validatenull(obj)) return
	try {
		obj = JSON.parse(obj)
	} catch {
		return obj
	}
	if (debug) {
		return obj
	}
	if (obj.dataType == 'string') {
		content = obj.content
	} else if (obj.dataType == 'number') {
		content = Number(obj.content)
	} else if (obj.dataType == 'boolean') {
		content = eval(obj.content)
	} else if (obj.dataType == 'object') {
		content = obj.content
	}
	return content
}
/**
 * 删除localStorage
 */
export const removeStore = (params = {}) => {
	let {
		name,
		type
	} = params
	name = keyName + name
	if (type) {
		uni.removeStorageSync(name)
	} else {
		uni.removeStorageSync(name)
	}

}


/**
 * 清空全部localStorage
 */
export const clearStore = (params = {}) => {
	let {
		type
	} = params
	if (type) {
		uni.clearStorageSync()
	} else {
		uni.clearStorageSync()
	}

}